package atguigu.forkjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;

public class CountTask extends RecursiveTask<Integer> {

	// 这是阈值，小于等于这个值，就不在进行分割了
	private static final int THREAD_HOLD = 2;

	private int start;
	private int end;

	public CountTask(int start, int end) {
		this.start = start;
		this.end = end;
	}

	@Override
	protected Integer compute() {
		int sum = 0;
		// 如果任务足够小就计算
		boolean canCompute = (end - start) <= THREAD_HOLD;
		if (canCompute) {
			// 直接进行计算，不在分割
			for (int i = start; i <= end; i++) {
				sum += i;
			}
		} else {
			// 分割
			int middle = (start + end) / 2;
			CountTask left = new CountTask(start, middle);
			CountTask right = new CountTask(middle + 1, end);

			// 执行子任务
			left.fork();
			right.fork();

			// 获取子任务结果
			int lResult = left.join();
			int rResult = right.join();
			sum = lResult + rResult;

//			CountTask task = new CountTask(start,end);
//			task.fork();
//			sum = task.join();

		}
		return sum;
	}

	public static void main(String[] args) {
		ForkJoinPool pool = new ForkJoinPool();
		CountTask task = new CountTask(1, 100);
		Future<Integer> result = pool.submit(task);
		try {
			System.out.println(result.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
}